/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.search.impl;

import java.util.List;

import org.grayrabbit.cms.dao.search.HotKeywordRuleDao;
import org.grayrabbit.cms.entity.HotKeywordRule;
import org.grayrabbit.cms.vo.search.HotKeywordRuleBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>HotKeywordRule</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 14:51:23 
 */
@Repository
public class HotKeywordRuleDaoImpl extends GenericHibernateDao<HotKeywordRule, String>
		implements HotKeywordRuleDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From HotKeywordRule as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see HotKeywordRuleDao#assembly(HotKeywordRuleBean)
	 */
	public QueryCondition assembly(HotKeywordRuleBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see HotKeywordRuleDao#assemblyCount(HotKeywordRuleBean)
	 */
	public QueryCondition assemblyCount(HotKeywordRuleBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see HotKeywordRuleDao#count(HotKeywordRuleBean)
	 */
	public int count(HotKeywordRuleBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see HotKeywordRuleDao#assemblyNameQuery(HotKeywordRuleBean)
	 */
	public QueryCondition assermblyNameQuery(HotKeywordRuleBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see HotKeywordRuleDao#assemblyNameQueryCount(HotKeywordRuleBean)
	 */
	public QueryCondition assemblyNameQueryCount(HotKeywordRuleBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see HotKeywordRuleDao#findByPage(Page, HotKeywordRuleBean)
	 */
	public List<HotKeywordRule> findByPage(Page<HotKeywordRule> page,
			HotKeywordRuleBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append(" order by obj.itemCode");
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(HotKeywordRuleBean bean,QueryCondition qc){
		//TODO:兄弟们此处根据业务修改代码，完成后，请删除本行，沈永生于${data}提醒!
		if(bean!=null){
			             if(bean.getItemCode() != null){
				qc.add("And obj.itemCode like ? ", bean.getItemCode()+"%");
			}
            if(bean.getItemName() != null){
				qc.add("And obj.itemName like ? ", bean.getItemName()+"%");
			}
            if(bean.getItemValue() != null){
				qc.add("And obj.itemValue like ? ", bean.getItemValue()+"%");
			}

		}
	}


}